package View;


import Controller.Beat.BeatController;
import Controller.Heart.*;
import Model.Beat.BeatModel;
import Model.Heart.HeartModel;
import View.Beat.* ; 
import View.Heart.* ; 
import Model.Game.* ; 
import Controller.Game.* ; 

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class DJView implements ActionListener,  BeatObserver, BPMObserver {
	BeatModelInterface model;
	CircleBar Cir;
	ControllerInterface controller;
	Imagen Im;
    JFrame viewFrame;
    JPanel viewPanel;
    JPanel timerPanel;
	BeatBar beatBar;
	JLabel bpmOutputLabel;
	JLabel timerOutputLabel;
    JFrame controlFrame;
    JPanel controlPanel;
    JLabel bpmLabel;
    JTextField bpmTextField;
    JButton setBPMButton;
    JButton increaseBPMButton;
    JButton decreaseBPMButton;
    JMenuBar menuBar;
    JMenuBar menuBarBeatBar ; 
    JMenu menu;
    JPanel F;
    JLayeredPane L;
    JMenu menuBeatBar ; 
    JMenuItem beatBarMenuItem ; 
    JMenuItem heartBarMenuItem ; 
    JMenuItem gameMenuItem ; 
    JMenuItem startMenuItem;
    JMenuItem stopMenuItem;

    public DJView(ControllerInterface controller, BeatModelInterface model) {	
		this.controller = controller;
		this.model = model;
		model.registerObserver((BeatObserver)this);
		model.registerObserver((BPMObserver)this);
    }
 public void createView3() {
    	
		// Create all Swing components here
        viewPanel = new JPanel(new GridLayout(1, 2));
        viewFrame = new JFrame("View");
        viewFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        viewFrame.setSize(new Dimension(350,250));
       
        JPanel bpmPanel = new JPanel(new GridLayout(5, 1));
        menuBarBeatBar = new JMenuBar() ; 
        menuBeatBar = new JMenu("Model") ;
        beatBarMenuItem = new JMenuItem("BeatModel");
        menuBeatBar.add(beatBarMenuItem);
        beatBarMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	viewFrame.dispose() ; 
            	controlFrame.dispose(); 
            	controller.stop() ; 
            	model = new BeatModel() ;
            	controller = new BeatController(model) ; 
            }
        });
        heartBarMenuItem = new JMenuItem("HeartModel");
        menuBeatBar.add(heartBarMenuItem); 
        heartBarMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	viewFrame.dispose() ;
            	controlFrame.dispose(); 
            	controller.stop() ; 
            	HeartModelInterface heart = HeartModel.getInstance();
            	model = new HeartAdapter(heart) ; 
            	controller = new HeartController(heart);	
            }
        });
        gameMenuItem = new JMenuItem("GameModel");
        menuBeatBar.add(gameMenuItem)  ;
        gameMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	viewFrame.dispose() ; 
            	controlFrame.dispose() ; 
            	controller.stop() ; 
            	model = new GameModel() ; 
            	controller = new GameController(model) ; 
            	}
        });
        JMenuItem gameMenuItemNewView = new JMenuItem("GameModel (New View) ") ;
        menuBeatBar.add(gameMenuItemNewView) ;
        gameMenuItemNewView.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent event) {
        	
        		controlFrame.dispose()  ; 
        		controller.stop() ; 
        		model = new GameModel() ; 
            	controller = new GameController(model) ; 
            	viewFrame.dispose(); 
            	createView2() ; 
        	}
        }) ;
        menuBarBeatBar.add(menuBeatBar);
        viewFrame.setJMenuBar(menuBarBeatBar);
        
        bpmOutputLabel = new JLabel("offline", SwingConstants.CENTER);
		Cir = new CircleBar();
		//beatBar = new BeatBar();
		//beatBar.setValue(0);
        //JPanel bpmPanel = new JPanel(new GridLayout(7, 1));
		//bpmPanel.add(beatBar);
		//bpmPanel.add(Cir);
        
        if(controller instanceof GameController){
        	timerOutputLabel = new JLabel("Time left: 10 seg.", SwingConstants.CENTER);
        	bpmOutputLabel = new JLabel("Current Clicks: 0", SwingConstants.CENTER);
        	bpmPanel.add(timerOutputLabel);
        }
        else{
        	bpmOutputLabel = new JLabel("offline", SwingConstants.CENTER);
        }
		beatBar = new BeatBar();
		Im = new Imagen();
		Cir= new CircleBar();
		beatBar.setValue(0);
        bpmPanel.add(bpmOutputLabel);
        viewPanel.add(bpmPanel);
		viewPanel.add(Cir);
       // viewPanel.add(Cir);
        viewFrame.getContentPane().add(viewPanel, BorderLayout.CENTER);
        //viewFrame.pack();
        viewFrame.setVisible(true);
	}
    public void createView(){
    	//F = new JPanel(new GridLayout(1, 1));
    	Im = new Imagen();
    	bpmOutputLabel = new JLabel("offline",SwingConstants.CENTER);
    	Cir = new CircleBar();
    	bpmOutputLabel.setOpaque(true);
    	bpmOutputLabel.setVerticalAlignment(JLabel.BOTTOM);
    	//L = new JLayeredPane();
    	//L.add(Im);
    	//L.add(bpmOutputLabel);
    	//L.moveToFront(bpmOutputLabel);
    	///L.moveToBack(Im);
    	//F.add(Im);
    	//F.add(Cir);
    	viewPanel = new JPanel(new GridLayout(1, 2));
    	viewFrame = new JFrame();
    	viewFrame.setSize(300,300);
    	//viewPanel.add(bpmOutputLabel);
    	//viewPanel.add(F);
        viewFrame.add(Im);
    	//viewFrame.add(viewPanel);
    	viewFrame.add(Cir);
    	viewFrame.setVisible(true);
    	viewFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void createView2() {
    	
		// Create all Swing components here
        viewPanel = new JPanel(new GridLayout(2, 2));
        viewFrame = new JFrame("View");
        viewFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // viewFrame.setSize(new Dimension(100,80));
       
        JPanel bpmPanel = new JPanel(new GridLayout(2, 1));
        menuBarBeatBar = new JMenuBar() ; 
        menuBeatBar = new JMenu("Model") ;
        beatBarMenuItem = new JMenuItem("BeatModel");
        menuBeatBar.add(beatBarMenuItem);
        beatBarMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	viewFrame.dispose() ; 
            	controlFrame.dispose(); 
            	controller.stop() ; 
            	model = new BeatModel() ;
            	controller = new BeatController(model) ; 
            }
        });
        heartBarMenuItem = new JMenuItem("HeartModel");
        menuBeatBar.add(heartBarMenuItem); 
        heartBarMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	viewFrame.dispose() ;
            	controlFrame.dispose(); 
            	controller.stop() ; 
            	HeartModelInterface heart = HeartModel.getInstance();
            	model = new HeartAdapter(heart) ; 
            	controller = new HeartController(heart);	
            }
        });
        gameMenuItem = new JMenuItem("GameModel");
        menuBeatBar.add(gameMenuItem)  ;
        gameMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	viewFrame.dispose() ; 
            	controlFrame.dispose() ; 
            	controller.stop() ; 
            	model = new GameModel() ; 
            	controller = new GameController(model) ; 
            	}
        });

        menuBarBeatBar.add(menuBeatBar);
        viewFrame.setJMenuBar(menuBarBeatBar);
        
        bpmOutputLabel = new JLabel("offline", SwingConstants.CENTER);
		Cir = new CircleBar();
		//beatBar = new BeatBar();
		//beatBar.setValue(0);
        //JPanel bpmPanel = new JPanel(new GridLayout(7, 1));
		//bpmPanel.add(beatBar);
		//bpmPanel.add(Cir);
        
        if(controller instanceof GameController){
        	timerOutputLabel = new JLabel("Time left: 10 seg.", SwingConstants.CENTER);
        	bpmOutputLabel = new JLabel("Current Clicks: 0", SwingConstants.CENTER);
        	viewPanel.add(timerOutputLabel);
        }
        else{
        	bpmOutputLabel = new JLabel("offline", SwingConstants.CENTER);
        }
		beatBar = new BeatBar();
		beatBar.setValue(0);
		bpmPanel.add(beatBar);
        bpmPanel.add(bpmOutputLabel);
        viewPanel.add(bpmPanel);
       // viewPanel.add(Cir);
        viewFrame.getContentPane().add(viewPanel, BorderLayout.CENTER);
        viewFrame.pack();
        viewFrame.setVisible(true);
	}
  
  
    public void createControls() {
		// Create all Swing components here
        //JFrame.setDefaultLookAndFeelDecorated(true);
        controlFrame = new JFrame("Control");
        controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        controlFrame.setSize(new Dimension(100, 80));

        controlPanel = new JPanel(new GridLayout(1, 2));

        menuBar = new JMenuBar();
        menu = new JMenu("DJ Control");
        startMenuItem = new JMenuItem("Start");
        menu.add(startMenuItem);
        startMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                controller.start();
            }
        });
        stopMenuItem = new JMenuItem("Stop");
        menu.add(stopMenuItem); 
        stopMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                controller.stop();
            }
        });
        JMenuItem exit = new JMenuItem("Quit");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        menu.add(exit);
        menuBar.add(menu);
        controlFrame.setJMenuBar(menuBar);

        bpmTextField = new JTextField(2);
        bpmLabel = new JLabel("Enter BPM:", SwingConstants.RIGHT);
        
        if(controller instanceof GameController) {
        	decreaseBPMButton = new JButton("Reset");
        	setBPMButton = new JButton("Click Me");}
        else{
        	decreaseBPMButton = new JButton("<<");
        	setBPMButton = new JButton("Set");}
        setBPMButton.setSize(new Dimension(10,40));
        increaseBPMButton = new JButton(">>");
        
        setBPMButton.addActionListener(this);
        increaseBPMButton.addActionListener(this);
        decreaseBPMButton.addActionListener(this);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 2));

		buttonPanel.add(decreaseBPMButton);
		buttonPanel.add(increaseBPMButton);

        JPanel enterPanel = new JPanel(new GridLayout(1, 2));
        enterPanel.add(bpmLabel);
        enterPanel.add(bpmTextField);
        JPanel insideControlPanel = new JPanel(new GridLayout(3, 1));
        insideControlPanel.add(enterPanel);
        insideControlPanel.add(setBPMButton);
        insideControlPanel.add(buttonPanel);
        controlPanel.add(insideControlPanel);
        
        bpmLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        bpmOutputLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        controlFrame.getRootPane().setDefaultButton(setBPMButton);
        controlFrame.getContentPane().add(controlPanel, BorderLayout.CENTER);

        controlFrame.pack();
        controlFrame.setVisible(true);
    }

	public void enableStopMenuItem() {
    	stopMenuItem.setEnabled(true);
	}

	public void disableStopMenuItem() {
    	stopMenuItem.setEnabled(false);
	}

	public void enableStartMenuItem() {
    	startMenuItem.setEnabled(true);
	}

	public void disableStartMenuItem() {
    	startMenuItem.setEnabled(false);
	}

    public void actionPerformed(ActionEvent event) {
		if (event.getSource() == setBPMButton) {
			if(controller instanceof GameController)
			{
				((GameController)controller).increaseBPM();			
			}
		else{
			int bpm = Integer.parseInt(bpmTextField.getText());
        	controller.setBPM(bpm);}
		} else if (event.getSource() == increaseBPMButton) {			
				controller.increaseBPM();
		} else if (event.getSource() == decreaseBPMButton) {
			if(controller instanceof GameController)
				((GameController)controller).reset();
			else
				controller.decreaseBPM();
		}
    }

	public void updateBPM() {
		if (model != null) {
			int bpm = model.getBPM();
			if (bpm == 0) {
				if (bpmOutputLabel != null) {

					if( this.controller instanceof GameController){
						bpmOutputLabel.setText("Current Clicks: 0");
		    			}
					else
        			bpmOutputLabel.setText("offline");
				}
			} else {
				if (bpmOutputLabel != null) {
					
					if( this.controller instanceof GameController){
						timerOutputLabel.setText("Time Left: " + ((GameModel)model).getCurrentTime() + " seg.");
						bpmOutputLabel.setText("Current clicks: " + model.getBPM());
		    			}
					else
					    bpmOutputLabel.setText("Current BPM: " + model.getBPM());
				}
			}
		}
	}
  
	public void updateBeat() {
		if (beatBar != null) {
			 beatBar.setValue(100);
			 
		}
		if(Cir != null)
		{
			Cir.setValue(-70);
		}
	}

}
